package org.despair2D.ui.supportClasses 
{
	import org.despair2D.debug.Logger;
	import org.despair2D.ui.events.InteractiveEvent;
	import org.despair2D.ui.events.ManipulateEvent;
	import org.despair2D.ui.puppet.IPuppet;
	import org.despair2D.ui.DespairUI;
	import org.despair2D.ui.SectionButton;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	

	///未选中(1-3) - 未选中无效(4) - 选中(5-7) - 选中无效(8)
public class AbstractToggleButton extends SectionButton 
{
	
	public function AbstractToggleButton( classRef:*, selected:Boolean = false, needClick:Boolean = true ) 
	{
		m_selected   =  selected;
		m_needClick  =  needClick;
		
		super(classRef);
		
		if (needClick)
		{
			this.addEventListener(ManipulateEvent.CLICK, ____onSelectChange);
			addPressEffectListener(onPress);	
		}
		
		else
		{
			this.addEventListener(ManipulateEvent.PRESS, ____onSelectChange);
		}
		
		addLeaveEffectListener(onLeave);
		addHoverEffectListener(onOver);
		addDisableEffectListener(onDisable)
	}
	
	
	
	final public function get selected() : Boolean
	{
		return m_selected;
	}
	
	public function set selected( b:Boolean ) : void
	{
		if (m_selected != b && this.interactive)
		{
			m_selected = b;
			
			if(m_anime == DespairUI.overPuppet)
			{
				m_overListener.execute();
			}
			
			else
			{
				m_leaveListener.execute();
			}
			
			
			this._selectedChangeAction();
		}
	}
	
	
	
	override public function setButtonSection( classRef:* ) : void
	{
		super.setButtonSection(classRef)
		
		if (!m_buttonSection.doesPointerNull(1))
		{
			Logger.reportError(m_buttonSection, 'get buttonSection', '错误按钮片段 (' + m_buttonSection.m_name + ')');
		}
		
		m_overPointer     =  m_buttonSection.doesPointerNull(2) ? 2 : m_leavePointer;
		m_pressPointer    =  m_buttonSection.doesPointerNull(3) ? 3 : m_overPointer;
		m_disablePointer  =  m_buttonSection.doesPointerNull(4) ? 4 : 1;
		
		m_leaveSelectedPointer    =  m_buttonSection.doesPointerNull(5) ? 5 : m_leavePointer;
		m_overSelectedPointer     =  m_buttonSection.doesPointerNull(6) ? 6 : m_leaveSelectedPointer;
		m_pressSelectedPointer    =  m_buttonSection.doesPointerNull(7) ? 7 : m_overSelectedPointer;
		m_disableSelectedPointer  =  m_buttonSection.doesPointerNull(8) ? 8 : 5;
		
		if (m_selected)
		{
			m_anime.pointer = this.interactive ? m_leaveSelectedPointer : m_disableSelectedPointer;
		}
		
		else
		{
			m_anime.pointer = this.interactive ? m_leavePointer : m_disablePointer;
		}
	}

	
	
	
	//======================
	// Internal
	//======================
	
	
	/**
	 * @inheritDoc
	 * 初期化傀儡
	 */
	override ns_despair function _addListeners() : void
	{
		if (m_needClick)
		{
			this.addEventListener(ManipulateEvent.PRESS,pressEffect);
		}
		
		this.addEventListener(ManipulateEvent.OVER,overEffect);
		this.addEventListener(ManipulateEvent.LEAVE,leaveEffect);
		this.addEventListener(ManipulateEvent.RELEASE, releaseEffect);
		this.addEventListener(InteractiveEvent.INTERACTIVE_CHANGE, disableEffect);
	}
	
	
	final ns_despair function onOver():void
	{
		m_anime.pointer = !m_selected ? m_overPointer : m_overSelectedPointer;
	}
	
	
	final ns_despair function onLeave():void
	{
		m_anime.pointer = !m_selected ? m_leavePointer : m_leaveSelectedPointer;
	}

	
	final ns_despair function onPress() : void
	{
		m_anime.pointer = !m_selected ? m_pressPointer : m_pressSelectedPointer;
	}
	
	
	final ns_despair function onDisable() : void
	{
		m_anime.pointer = !m_selected ? m_disablePointer : m_disableSelectedPointer
	}
	
	
	/**
	 * @inheritDoc
	 * 选中状态变化后的行为
	 */
	protected function _selectedChangeAction() : void
	{	
	}
	

	
	/**
	 * @inheritDoc
	 * 选中状态变化
	 */
	final ns_despair function ____onSelectChange( e:ManipulateEvent ) : void
	{
		this.selected = !this.selected;
	}
	
	
	
	ns_despair var m_leavePointer:int = 1;
	
	ns_despair var m_overPointer:int;
	
	ns_despair var m_pressPointer:int;
	
	ns_despair var m_disablePointer:int;
	
	ns_despair var m_leaveSelectedPointer:int;
	
	ns_despair var m_overSelectedPointer:int;
	
	ns_despair var m_pressSelectedPointer:int;

	ns_despair var m_disableSelectedPointer:int;
	
	ns_despair var m_selected:Boolean;

	ns_despair var m_needClick:Boolean;
	
}

}